package com.liqi.web.controller.question;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.liqi.common.core.controller.BaseController;
import com.liqi.common.core.domain.R;
import com.liqi.common.core.page.TableDataInfo;
import com.liqi.common.utils.StringUtils;
import com.liqi.question.domain.Question;
import com.liqi.question.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: lgh
 * @Date: 2023-02-23
 * @Description:
 */
@RequestMapping("/question")
@RestController
@Api(tags = "问答表")
public class QuestionController extends BaseController {

    @Autowired
    private QuestionService questionService;

    @GetMapping("/questionList")
    @ApiOperation(value = "问答表list")
    public TableDataInfo questionList(@ApiParam(value = "问题") @RequestParam(value = "question",required = false)String question
            , @ApiParam(value = "当前页",required = true)@RequestParam("pageNum") Integer pageNum
            , @ApiParam(value = "大小",required = true)@RequestParam("pageSize")Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Question> qw = new QueryWrapper<>();
        LambdaQueryWrapper<Question> lambda = qw.lambda();
        lambda.like(StringUtils.isNotBlank(question),Question::getQuestion,question).orderByDesc(Question::getId);
        List<Question> list = questionService.list(lambda);
        return getDataTable(list);
    }

    @PostMapping("/questionAdd")
    @ApiOperation(value = "问答表添加")
    public R questionAdd(@Validated  @RequestBody Question question){
        questionService.save(question);
        return R.ok("添加问题成功",null);
    }


    @PostMapping("/questionUp")
    @ApiOperation(value = "问答表更新")
    public R questionUp(@Validated  @RequestBody Question question){
        questionService.updateById(question);
        return R.ok("修改问题成功",null);
    }

    @PostMapping("/questionDel")
    @ApiOperation(value = "问答表删除")
    public R questionDel(@RequestBody List<Integer> ids){
        questionService.removeByIds(ids);
        return R.ok("删除问题成功",null);
    }
}
